package com.fzhucc.modules.lesson.rest;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.lesson.domain.LessonIcon;
import com.fzhucc.modules.lesson.service.LessonIconService;
import com.fzhucc.modules.lesson.service.dto.LessonIconQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-15
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程:分类管理")
@RequestMapping("/api/lessonIcon")
public class LessonIconController {

    private final LessonIconService lessonIconService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('lessonIcon:list')")
    public void exportLessonIcon(HttpServletResponse response, LessonIconQueryCriteria criteria) throws IOException {
        lessonIconService.download(lessonIconService.queryAll(criteria), response);
    }

    @AnonymousGetMapping("/show")
    @Log("查询lessonIcon")
    @ApiOperation("查询lessonIcon")
    public ResponseEntity<Object> queryLessonIcon(LessonIconQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonIconService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增lessonIcon")
    @ApiOperation("新增lessonIcon")
    @PreAuthorize("@el.check('lessonIcon:add')")
    public ResponseEntity<Object> createLessonIcon(@Validated @RequestBody LessonIcon resources){
        return new ResponseEntity<>(lessonIconService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lessonIcon")
    @ApiOperation("修改lessonIcon")
    @PreAuthorize("@el.check('lessonIcon:edit')")
    public ResponseEntity<Object> updateLessonIcon(@Validated @RequestBody LessonIcon resources){
        lessonIconService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除lessonIcon")
    @ApiOperation("删除lessonIcon")
    @PreAuthorize("@el.check('lessonIcon:del')")
    public ResponseEntity<Object> deleteLessonIcon(@RequestBody Integer[] ids) {
        lessonIconService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}